
// 饼状图公共类
export default class PieEchart {
  // type = 'pie' 饼状图 || loop 环形图
  constructor(legendData = [], seriesData = [], title = '', type = 'pie', center = ['50%', '50%'], radius = ['30%', '60%']) {
    this.pieOption.legend.data = legendData;
    this.pieOption.series[0].data = seriesData;
    this.pieOption.title.text = title;
    this.pieOption.series[0].name = title;
    if (type == 'loop') {
      this.pieOption.series[0].center = center;
      this.pieOption.series[0].radius = radius;
    }
  }

  setTitleText(title) {
    this.pieOption.title.text = title;
    return this.pieOption;
  }

  setlegendData(legendData) {
    this.pieOption.legend.data = legendData;
    return this.pieOption;
  }

  setSeriesData(seriesData) {
    this.pieOption.series[0].data = seriesData;
    return this.pieOption;
  }

  setSeries(series) {
    this.pieOption.series = series;
    return this.pieOption;
  }

  setLegend(legend) {
    this.pieOption.legend = {
      ...this.pieOption.legend,
      ...legend,
    };
    return this.pieOption;
  }

  setTitle(title) {
    this.pieOption.title = {
      ...this.pieOption.title,
      ...title,
    };
    return this.pieOption;
  }

  setColor(color) {
    if (color) {
      this.pieOption.color = color;
      return this.pieOption;
    }
      return this.pieOption;
  }

  getOption() {
    return this.pieOption;
  }

  pieOption = {
    title: {
      text: '默认标题',
      x: 'center',
      top: 20,
    },
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)',
    },
    color: ['#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#546570', '#c4ccd3'],
    legend: {
      type: 'scroll',
      orient: 'vertical',
      right: 10,
      top: 30,
      bottom: 20,
      animation: true,
      selector: true,
      data: [],
      formatter: (name) => {
        if (name.length <= 8) {
          return name;
        } else if (name.length > 8 && name.length <= 16) {
          return `${name.slice(0, 8)}\n${name.slice(8, 16)}`;
        } else if (name.length > 16 && name.length <= 24) {
          return `${name.slice(0, 8)}\n${name.slice(8, 16)}\n${name.slice(16, 24)}`;
        } else if (name.length > 24 && name.length <= 32) {
          return `${name.slice(0, 8)}\n${name.slice(8, 16)}\n${name.slice(16, 24)}\n${name.slice(24)}`;
        } else if (name.length > 32) {
          return `${name.slice(0, 8)}\n${name.slice(8, 16)}\n${name.slice(16, 24)}\n${name.slice(24, 32)}...`;
        }
      },
    },
    series: [
      {
        name: '',
        id: '',
        type: 'pie',
        animationType: 'scale',
        radius: '55%',
        center: ['50%', '50%'],
        minAngle: 1,
        avoidLabelOverlap: true, // 是否启用防止标签重叠策略
        hoverAnimation: false,
        data: [],
        itemStyle: {
          emphasis: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
        label: {
          align: 'left',
          normal: {
            formatter(v) {
              const name = v.name;
              if (name.length <= 8) {
                return name;
              } else if (name.length > 8 && name.length <= 16) {
                return `${name.slice(0, 8)}\n${name.slice(8, 16)}`;
              } else if (name.length > 16 && name.length <= 24) {
                return `${name.slice(0, 8)}\n${name.slice(8, 16)}\n${name.slice(16, 24)}`;
              } else if (name.length > 24 && name.length <= 32) {
                return `${name.slice(0, 8)}\n${name.slice(8, 16)}\n${name.slice(16, 24)}\n${name.slice(24)}`;
              } else if (name.length > 32) {
                return `${name.slice(0, 8)}\n${name.slice(8, 16)}\n${name.slice(16, 24)}\n${name.slice(24, 32)}...`;
              }
            },
            textStyle: {
              // fontSize : 10
            },
          },
        },
      },
    ],
  }
}

